
import _RandomColor_ from 'randomcolor';

namespace ColorUtils {

    export interface RandomColorOptionsSingle {
        hue?: number | string | undefined;
        luminosity?: "bright" | "light" | "dark" | "random" | undefined;
        seed?: number | string | undefined;
        format?: "hsvArray" | "hslArray" | "hsl" | "hsla" | "rgbArray" | "rgb" | "rgba" | "hex" | undefined;
        alpha?: number | undefined;
    }

    export interface RandomColorOptionsMultiple extends RandomColorOptionsSingle {
        count: number;
    }

    export function randomColor(options?: RandomColorOptionsSingle | RandomColorOptionsMultiple): string {
        return _RandomColor_(options);
    }

    export function hexColorToHexNumber(colorString: string): number {

        // 去掉首位的 '#' 符号
        const cleanHex = colorString.slice(1);
        // 将十六进制字符串转换为数字
        const hexNumber = parseInt(cleanHex, 16);
        return hexNumber;
    }

    export function randomColorNumber(options?: RandomColorOptionsSingle): number {
        return hexColorToHexNumber(randomColor(options));
    }

    export function complementaryColorNumber(color: number): number {
        const rgb = getRGBFromHex(color)
        return hexColorToHexNumber(colorRGBToHex(Math.abs(255 - rgb.red), Math.abs(255 - rgb.green), Math.abs(255 - rgb.blue)));
    }

    export function calculateLuminance(r: number, g: number, b: number): number {
        return 0.2126 * r + 0.7152 * g + 0.0722 * b;
    }

    export function suggestContrastColor(color: number) {
        const rgb = getRGBFromHex(color)
        if (calculateLuminance(rgb.red, rgb.green, rgb.blue) > 0.179) {
            return 0x000000
        } else {
            return 0xffffff
        }
    }

    export function colorRGBToHex(red: number, green: number, blue: number): string {
        // 检查输入的颜色分量是否在有效范围内
        if (red < 0 || red > 255 || green < 0 || green > 255 || blue < 0 || blue > 255) {
            throw new Error('Color components must be in the range 0 to 255');
        }

        // 将每个颜色分量转换为两位的十六进制字符串，并确保它们是两位的
        const redHex = padStart(red.toString(16), 2);
        const greenHex = padStart(green.toString(16), 2);
        const blueHex = padStart(blue.toString(16), 2);

        // 拼接成最终的十六进制颜色值
        return `#${redHex}${greenHex}${blueHex}`;
    }


    export function getRGBFromHex(hex: number): { red: number, green: number, blue: number } {
        // 确保输入的是有效的十六进制数
        if (typeof hex !== 'number') {
            throw new Error('Input must be a number');
        }
        // 将数字转换为十六进制字符串并去掉前缀 "0x"
        const hexString = padStart(hex.toString(16), 6);
        // 提取红色、绿色和蓝色分量
        const red = parseInt(hexString.substring(0, 2), 16);
        const green = parseInt(hexString.substring(2, 4), 16);
        const blue = parseInt(hexString.substring(4, 6), 16);
        return { red, green, blue };
    }

    export function padStart(value: string, length: number, target: string = '0') {
        if (value.length >= length) return value;
        if (target.length > 1) target = target[0];
        return new Array(length - value.length).fill(target).join('') + value;
    }
}

export default ColorUtils;
